package com.yami.shop.admin.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.OrderStatus;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.bean.model.OrderRefund;
import com.yami.shop.bean.param.OrderParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/order/orderRefund")
public class OrderRefundController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private UserAddrOrderService userAddrOrderService;

    @Autowired
    private ProductService productService;

    @Autowired
    private SkuService skuService;



    /**
     * 退款订单查询
     */
//    @GetMapping("/refundPage")
//    @PreAuthorize("@pms.hasPermission('order:orderRefund:refundPage')")
//    public ResponseEntity<IPage<Order>> refundPage(OrderParam orderParam,PageParam<Order> page) {
//        Long shopId = SecurityUtils.getSysUser().getShopId();
//        orderParam.setShopId(shopId);
//        orderParam.setRefundState(1);
//        IPage<Order> orderIPage = orderService.pageOrdersRefundByOrderParam(page, orderParam);
//        return ResponseEntity.ok(orderIPage);
//    }

    /**
     * 退款订单查询
     */
    @GetMapping("/refundPage")
    @PreAuthorize("@pms.hasPermission('order:orderRefund:refundPage')")
    public ResponseEntity<IPage<OrderRefund>> refundPage(OrderParam orderParam,PageParam<OrderRefund> page) {
        Long shopId = SecurityUtils.getSysUser().getShopId();
        orderParam.setShopId(shopId);
        IPage<OrderRefund> orderIPage = orderService.pageOrdersRefundByOrderParam(page, orderParam);
        return ResponseEntity.ok(orderIPage);
    }




//    /**
//     * 更新退款审核状态
//     */
//    @PutMapping("/refundState")
//    @PreAuthorize("@pms.hasPermission('order:orderRefund:refundState')")
//    public ResponseEntity<Void> refundState(@Valid Order order) throws Exception {
//        int status = order.getStatus();
//        List<Order> orders = orderService.OrderAndOrderItems(OrderStatus.REFUND.value(),order.getOrderNumber());
//        if (CollectionUtil.isEmpty(orders)) {
//            return null;
//        }
//        orders.get(0).setStatus(status);
//        orderService.updatebyOrderNumber(orders);
//        if(status==8){
//            RefundController refundController = new RefundController();
//            refundController.refund(order.getOrderNumber());
//        }
//
//        List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(order.getOrderNumber());
//        for (OrderItem orderItem : orderItems) {
//            productService.removeProductCacheByProdId(orderItem.getProdId());
//            skuService.removeSkuCacheBySkuId(orderItem.getSkuId(),orderItem.getProdId());
//        }
//        return ResponseEntity.ok().build();
//    }


}
